﻿using AnlianCommon.Common.Domains.Addresses;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AnlianCommon.API.Commands.CommonRegion
{
    public class ProvinceQueryHandler : IRequestHandler<ProvinceQueryCommand, Result>
    {
        private readonly IBaseRepository<Province> _provinceRepository;

        public ProvinceQueryHandler(IBaseRepository<Province> provinceRepository)
        {
            _provinceRepository = provinceRepository;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(ProvinceQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var provinceList = await (await _provinceRepository.GetAll()).ToListAsync();
                result.Data = provinceList;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
